package com.hk.kindheartedness;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.hk.kindheartedness.db.DBOpenHelper;
import com.hk.kindheartedness.util.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 患者查看医生
 */
public class PatientQueryDoctorActivity extends AppCompatActivity {

    //    Initialize Toast
    private Toast toast;

    //    Initialize intent
    private Intent intent;

    //    声明DBOpenHelper对象
    private DBOpenHelper dbOpenHelper;

    //    Initialize ListView
    private ListView queryDoctorListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_query_doctor);
//        实例化
        dbOpenHelper = new DBOpenHelper(PatientQueryDoctorActivity.this, "db_kindheartedness", null, 1);
//        获取listView控制权
        queryDoctorListView = findViewById(R.id.queryDoctorListView);
//        查询所有医生并进行回显
        findAllDoctorInfo();
//        获取ListView点击事件并进行处理
        queryDoctorListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //                获取选项中的HashMap对象
                HashMap<String, String> map = (HashMap<String, String>) queryDoctorListView.getItemAtPosition(position);
                String doctorId = map.get("doctorId");
                Log.i("获取的医生ID为:",doctorId);
//                跳转到预约缴费页面
                intent = new Intent(PatientQueryDoctorActivity.this,PatientQueryDoctorInfoActivity.class);
//                当前选中的医生ID编号
                Bundle bundle = new Bundle();
                bundle.putString("doctorId",doctorId);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }

    /**
     * 查询所有医生并进行回显
     */
    private void findAllDoctorInfo() {
        SQLiteDatabase readableDatabase = dbOpenHelper.getReadableDatabase();
        //        存储获取到的数据
        List<Map<String, Object>> resultList = new ArrayList<>(6);
        Cursor queryDoctorDetails = readableDatabase.query("doctor_details", null, null, null, null, null, null);
        while (queryDoctorDetails.moveToNext()) {
            //        存储临时Map对象
            Map<String, Object> map = new HashMap<>(6);
            String id = queryDoctorDetails.getString(0);
            String userName = queryDoctorDetails.getString(1);
//            职务ID
            String positionId = queryDoctorDetails.getString(2);
//            科室ID
            String departmentsId = queryDoctorDetails.getString(3);
            String describe = queryDoctorDetails.getString(5);
            map.put("doctorId",id);
            map.put("userName",userName);
            map.put("describe",describe);
            Cursor queryPosition = readableDatabase.query("position", null, "id = ?", new String[]{positionId}, null, null, null);
            while (queryPosition.moveToNext()) {
                String positionName = queryPosition.getString(1);
                map.put("positionName",positionName);
            }
            Cursor queryDepartments = readableDatabase.query("departments", null, "id = ?", new String[]{departmentsId}, null, null, null);
            while (queryDepartments.moveToNext()) {
                String departmentsName = queryDepartments.getString(1);
                map.put("departmentsName",departmentsName);
            }
            resultList.add(map);
        }
//        判断是否具有医生
        if (resultList == null || resultList.size() == 0) {
            toast = ToastUtil.toastHint(toast, PatientQueryDoctorActivity.this, "暂无医生，请稍后查看！");
            return;
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(
                PatientQueryDoctorActivity.this,
                resultList,
                R.layout.query_patient_doctor,
                new String[]{
                        "doctorId","userName","positionName",
                        "departmentsName", "describe",
                },
                new int[]{
                        R.id.doctorId,R.id.doctorName,R.id.positionName,
                        R.id.departmentsName, R.id.doctorDesc,
                }
        );
        queryDoctorListView.setAdapter(simpleAdapter);
    }


    /**
     * 断开数据库连接
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dbOpenHelper != null) {
            dbOpenHelper.close();
        }
    }

}